package com.shop.service.impl;

import com.shop.mapper.IMenuMapper;
import com.shop.pojo.CommonResult;
import com.shop.pojo.MenuTemplate;
import com.shop.service.IAuthService;
import com.shop.service.IMenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class AuthServiceImpl implements IAuthService {

    @Resource
    private IMenuMapper menuMapper;
    @Resource
    private IMenuService menuService;

    @Override
    public CommonResult<List<MenuTemplate>> getAuthListByType(String type) {

        if (Objects.equals("list", type)) {
            List<MenuTemplate> parent = menuMapper.getMenusByPID();
            for (int i = 0; i < parent.size(); i++) {
                getAuthList(parent, parent.get(i));
            }
            return new CommonResult<>(200, "success", parent);
        }
        return menuService.getMenus();
//        for (MenuTemplate auth : parent) {
//            getAuthList(parent, auth);
//        }


    }

    private void getAuthList(List<MenuTemplate> authList, MenuTemplate auth) {

        int id = auth.getId();
        List<MenuTemplate> menusByPID = menuMapper.getMenusByPID(id);
        for (int i = 0; i < menusByPID.size(); i++) {
            authList.add(menusByPID.get(i));
        }
//        authList.addAll(menusByPID);
        if (menusByPID.size() != 0) {
            for (MenuTemplate menuTemplate : menusByPID) {
                getAuthList(authList, menuTemplate);
            }
        }

    }

}
